#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "..//include//createvfs.h"
#include "..//include//n_ary.h"
#include "..//include//vfs_errorcodes.h"

Tree reqd_node_cur(char dir_name[]){


   char str[100];
   char newstr1[50];
   char newstr2[50]; 
   char node_label[50];  
   char abspath[100]=""; 

   int num;
   int j=0;
   int level=0;
   int count=1;
   int pos=1;
   int i; 
   int k=0;    
   
   Tree cur_tree;   
   Tree prev_tree;
   Tree prev_prev_tree;
   Tree sib=cur_tree;
   Tree new;
   Tree temp;
   Tree prev_sib;
   

   NODE first=NULL;   
   NODE cur=first;
   NODE last;

	while(dir_name[pos]!='\0'){
         strcat(abspath,"/");	
         j=0;
         num=split(dir_name,pos);
         
         if(num>(strlen(dir_name)-1)) num = strlen(dir_name)-1;
         
         for(i=pos; i<=num; i++){
            newstr2[j]=dir_name[i];
            j++;
         }
         newstr2[j]='\0';    	 
                 
         first=insert_rear_ll(newstr2,abspath,first);
         strcat(abspath,newstr2);
         if(num>=strlen(dir_name)-1) break;
         pos=num+2;
        
      }

      last=cur;
      cur=first;
      while(cur->next!=NULL){
        count=count+1;
        cur=cur->next;
      } 
      
     
      
      cur=first;
      cur_tree=root->left;
      prev_sib=root;
      sib=root->left;
      prev_tree=root;   
      prev_prev_tree=NULL; 
      //printf("Root: %s\n",root->node_label);
      while(count>0){
	  if(cur_tree!=NULL){ 
	  	//printf("%s\n",cur_tree->node_label);
        	if(strcmp(cur_tree->node_label,cur->entry) == 0){ 
        	   if(count!=1){ 
            		cur=cur->next;
            		if(count!=1)            		
            			prev_prev_tree=cur_tree;
            		//printf("\nCHECK CHECK: %s\n",prev_prev_tree->node_label);}
            		prev_tree=cur_tree;
			cur_tree=cur_tree->left;
			prev_sib=sib;
			sib=cur_tree;
			count=count-1;	
		   }
		   else
		        count=count-1;		
			
        	}
        	else{   
        		//prev_prev_tree=prev_tree;
			prev_tree=cur_tree;
            		cur_tree=cur_tree->right;
        	}
	  }
	  else{   
		//printf("\nlistdir_FAILURE\t%s",ERR_VFS_LISTDIR_01); // Cannot find path/dir
        	return NULL;  			      
	  }
     
      } 
      //printf("Another check: %s",prev_tree->node_label);
      if(cur_tree==sib)
      	return cur_tree;
      else
      	return cur_tree;
      
 }
 
 
 
 
 
 
 
 Tree reqd_node_prev(char dir_name[]){


   char str[100];
   char newstr1[50];
   char newstr2[50]; 
   char node_label[50];  
   char abspath[100]=""; 

   int num;
   int j=0;
   int level=0;
   int count=1;
   int pos=1;
   int i; 
   int k=0;    
   
   Tree cur_tree;   
   Tree prev_tree;
   Tree prev_prev_tree;
   Tree sib=cur_tree;
   Tree new;
   Tree temp;
   Tree prev_sib;
   

   NODE first=NULL;   
   NODE cur=first;
   NODE last;

	while(dir_name[pos]!='\0'){
         strcat(abspath,"/");	
         j=0;
         num=split(dir_name,pos);
         
         if(num>(strlen(dir_name)-1)) num = strlen(dir_name)-1;
         
         for(i=pos; i<=num; i++){
            newstr2[j]=dir_name[i];
            j++;
         }
         newstr2[j]='\0';    	 
                 
         first=insert_rear_ll(newstr2,abspath,first);
         strcat(abspath,newstr2);
         if(num>=strlen(dir_name)-1) break;
         pos=num+2;
        
      }

      last=cur;
      cur=first;
      while(cur->next!=NULL){
        count=count+1;
        cur=cur->next;
      } 
      
     
      
      cur=first;
      cur_tree=root->left;
      prev_sib=root;
      sib=root->left;
      prev_tree=root;   
      prev_prev_tree=NULL; 
      //printf("Root: %s\n",root->node_label);
      while(count>0){
	  if(cur_tree!=NULL){ 
	  	//printf("%s\n",cur_tree->node_label);
        	if(strcmp(cur_tree->node_label,cur->entry) == 0){ 
        	   if(count!=1){ 
            		cur=cur->next;
            		if(count!=1)           		
            			prev_prev_tree=cur_tree;
            		//printf("\nCHECK CHECK: %s\n",prev_prev_tree->node_label);}
            		prev_tree=cur_tree;
			cur_tree=cur_tree->left;
			prev_sib=sib;
			sib=cur_tree;
			count=count-1;	
		   }
		   else
		        count=count-1;		
			
        	}
        	else{   
        		//prev_prev_tree=prev_tree;
			prev_tree=cur_tree;
            		cur_tree=cur_tree->right;
        	}
	  }
	  else{   
		//printf("\nlistdir_FAILURE\t%s",ERR_VFS_LISTDIR_01); // Cannot find path/dir
        	return NULL;  			      
	  }
     
      } 
      //printf("Another check: %s",prev_tree->node_label);
      if(cur_tree==sib)
      	return prev_tree;
      else
      	return prev_tree;
      
 }
 
 
 
 
 int reqd_node_isSib(char dir_name[]){ // 1-->sib/first node; 0-->Internal node


   char str[100];
   char newstr1[50];
   char newstr2[50]; 
   char node_label[50];  
   char abspath[100]=""; 

   int num;
   int j=0;
   int level=0;
   int count=1;
   int pos=1;
   int i; 
   int k=0;    
   
   Tree cur_tree;   
   Tree prev_tree;
   Tree prev_prev_tree;
   Tree sib=cur_tree;
   Tree new;
   Tree temp;
   Tree prev_sib;
   

   NODE first=NULL;   
   NODE cur=first;
   NODE last;

	while(dir_name[pos]!='\0'){
         strcat(abspath,"/");	
         j=0;
         num=split(dir_name,pos);
         
         if(num>(strlen(dir_name)-1)) num = strlen(dir_name)-1;
         
         for(i=pos; i<=num; i++){
            newstr2[j]=dir_name[i];
            j++;
         }
         newstr2[j]='\0';    	 
                 
         first=insert_rear_ll(newstr2,abspath,first);
         strcat(abspath,newstr2);
         if(num>=strlen(dir_name)-1) break;
         pos=num+2;
        
      }

      last=cur;
      cur=first;
      while(cur->next!=NULL){
        count=count+1;
        cur=cur->next;
      } 
      
     
      
      cur=first;
      cur_tree=root->left;
      prev_sib=root;
      sib=root->left;
      prev_tree=root;   
      prev_prev_tree=NULL; 
      //printf("Root: %s\n",root->node_label);
      while(count>0){
	  if(cur_tree!=NULL){ 
	  	//printf("%s\n",cur_tree->node_label);
        	if(strcmp(cur_tree->node_label,cur->entry) == 0){ 
        	   if(count!=1){ 
            		cur=cur->next;
            		if(count!=1)            		
            			prev_prev_tree=cur_tree;
            		//printf("\nCHECK CHECK: %s\n",prev_prev_tree->node_label);}
            		prev_tree=cur_tree;
			cur_tree=cur_tree->left;
			prev_sib=sib;
			sib=cur_tree;
			count=count-1;	
		   }
		   else
		        count=count-1;		
			
        	}
        	else{   
        		//prev_prev_tree=prev_tree;
			prev_tree=cur_tree;
            		cur_tree=cur_tree->right;
        	}
	  }
	  else{   
		printf("\nlistdir_FAILURE\t%s",ERR_VFS_LISTDIR_01); // Cannot find path/dir
        	return NULL;  			      
	  }
     
      } 
      
    
      //printf("Another check: %s",prev_tree->node_label);
      if(cur_tree==sib)
      	return 1;
      else
      	return 0;
      
 }
 
 
 
